//
//  NWHomeTableViewController.swift
//  微博01
//
//  Created by 牛威 on 15/12/12.
//  Copyright © 2015年 牛威. All rights reserved.
//

import UIKit
import SVProgressHUD

 /// 原微博的可重用标示符
let NWHomeTableCell = "NWHomeTableCell"

 /// 转发微博的可重用标示符
let NWHomeRetweedCellID  = "NWHomeRetweedsCellID"

class NWHomeTableViewController: NWVisitorTableViewController {
    

        //懒加载视图模型对象
        
        private lazy var homeModelViewModel = HomeModelViewModel()
        
        
        //MARK: - 设置nav的监听方法
        
        @objc  private  func searchFriends(){
            
            let vc = NWTempViewController()
            
            navigationController?.pushViewController(vc, animated: true)
            
            tabBarController?.tabBar.hidden = false
            
        }
        
        @objc  private  func pod(){
            
            print("pod")
            
        }
        
        
        
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            
            
            
            if !UserAccountViewModel.sharedAccount.isLogin {
                
                
                visitorView?.setVisitorViewInfo(nil, message:"最美的不是下雨天,而是与你一起发过微博的下午")
                
                return
                
            }
            
            setupUI()
            
            registerCell()

            loadData()
            
            
            
            
        }
        
        
        private func setupUI(){
            
            
            //设置左右上角的button
            
            navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: "searchFriends")
            
            navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "pod")
            
            
        }
        
        //MARK: -  注册表格
        
        private func registerCell (){
            
            tableView.registerClass(NWHomeNormalCell.self, forCellReuseIdentifier: NWHomeTableCell)
            tableView.registerClass(NWHomeRetweedCell.self, forCellReuseIdentifier: NWHomeRetweedCellID)
            
            //预估行高
            
             tableView.estimatedRowHeight = 400
            
//            tableView.rowHeight = UITableViewAutomaticDimension
//            tableView.rowHeight = 400
            
            //设置分割线样式----在bottom和top之间有一个小重影
            
           self.tableView.separatorStyle = .None
            
            //测试下拉刷新 (下拉刷新控件默认没有,需要实例化)
            
             refreshControl = NWDownRefresh()
            
            
            refreshControl!.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
            
            
            //上拉刷新视图

            tableView.tableFooterView = pullUpView
            
            
            
            
        }
        
        
       @objc private func loadData(){
            
            refreshControl?.beginRefreshing()
            //关闭上啦刷新动画
            pullUpView.stopAnimating()
        
            homeModelViewModel.loadHomeModelData(isPullup: pullUpView.isAnimating()) { (isSuccessed) -> () in
                
                self.refreshControl?.endRefreshing()
                
                if !isSuccessed {
                    
                    SVProgressHUD.showInfoWithStatus("您的网络不给力请稍后重试...")
                    
                    
                    return
                }
                
                self.tableView.reloadData()
                
                
                
            }
            
        }
    
    
        //MARK: -  上拉刷新视图
    
    private lazy var pullUpView : UIActivityIndicatorView =  {
    
    
       let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        
        indicator.color = UIColor.lightGrayColor()

        return indicator
    
    }()
}



//MARK: - 实现数据源方法

extension NWHomeTableViewController {


    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        //这里的arrayM在视图模型中写的是懒加载 失少要有数值 所以没有可选项判断
        
                return homeModelViewModel.arrayM.count
        
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        
        let array = homeModelViewModel.arrayM[indexPath.row]
        
        let cell = tableView.dequeueReusableCellWithIdentifier(array.cellID, forIndexPath: indexPath) as! NWHomeCell
        
        
        cell.arrayM = array
        
        //判断是否是最后一条微博
        
        if indexPath.row == homeModelViewModel.arrayM.count - 1 && !pullUpView.isAnimating() {
        
          pullUpView.startAnimating()
            
            //加载数据
            
            loadData()
        
            print("上啦刷新")
        
        }
        
        return cell
        
    }
    
    
   //代理方法 苹果官方文档指出:如果行高是固定值,就不要实现行高代理方法 这个方法调用次数过多,影响性能
    //实际开发中 行高一定要缓存
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
//        print("设置行高++++++\(indexPath)")
        //更新模型
        
        return  homeModelViewModel.arrayM[indexPath.row].rowHeight

        
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        print("点击cell")
        
        self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    
    
}


